package com.cheyun.netsalev3

import android.util.Log
import com.cheyun.netsalev3.config.GeTuiConfig
import com.cheyun.netsalev3.tencenttim.helper.ConfigHelper
import com.cheyun.netsalev3.tencenttim.signature.GenerateTestUserSig
import com.cheyun.netsalev3.tencenttim.thirdpush.ThirdPushTokenMgr
import com.cheyun.netsalev3.utils.api.GlobalConfig
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonToken
import com.hjq.gson.factory.GsonFactory
import com.hjq.gson.factory.JsonCallback
import com.huawei.android.hms.agent.HMSAgent
import com.igexin.sdk.BuildConfig
import com.igexin.sdk.PushManager
import com.meizu.cloud.pushsdk.util.MzSystemUtils
//import com.pgyer.pgyersdk.PgyerSDKManager
//import com.pgyersdk.crash.PgyCrashManager
import com.tencent.imsdk.session.SessionWrapper
import com.tencent.imsdk.utils.IMFunc
import com.tencent.qcloud.tim.uikit.TUIKit
import com.umeng.socialize.PlatformConfig

object PlatformSdk {

    private val TAG = PlatformSdk::class.java!!.getSimpleName()

    private var thirdCode: String = ""

    fun initPlatformSdk() {
        //初始化推送
//        JPushInterface.setDebugMode(true)
//        JPushInterface.init(this)

        //个推推送
        GeTuiConfig.init(MyApplication.getInstance())
        initGeTuiSdk()

        //异常捕获
//        PgyCrashManager.register()
//        initPgyerSDK()


//        腾讯 TIM
        //判断是否是在主线程
//        if (SessionWrapper.isMainProcess(MyApplication.getInstance().applicationContext)) {
//            /**
//             * TUIKit的初始化函数
//             *
//             * @param context  应用的上下文，一般为对应应用的ApplicationContext
//             * @param sdkAppID 您在腾讯云注册应用时分配的sdkAppID
//             * @param configs  TUIKit的相关配置项，一般使用默认即可，需特殊配置参考API文档
//             */
//            TUIKit.init(MyApplication.getInstance(), GenerateTestUserSig.SDKAPPID, ConfigHelper().getConfigs())
//
//            if (ThirdPushTokenMgr.USER_GOOGLE_FCM) {
//                thirdCode = "google"
//            } else if (IMFunc.isBrandXiaoMi()) {
//                // 小米离线推送
//                thirdCode = "xiaomi"
////                MiPushClient.registerPush(this, PrivateConstants.XM_PUSH_APPID, PrivateConstants.XM_PUSH_APPKEY)
//            } else if (IMFunc.isBrandHuawei()) {
//                // 华为离线推送
//                thirdCode = "huawei"
//                HMSAgent.init(MyApplication.getInstance())
//            } else if (MzSystemUtils.isBrandMeizu(MyApplication.getInstance())) {
//                // 魅族离线推送
//                thirdCode = "meizu"
////                com.meizu.cloud.pushsdk.PushManager.register(this, PrivateConstants.MZ_PUSH_APPID, PrivateConstants.MZ_PUSH_APPKEY)
//            } else if (IMFunc.isBrandVivo()) {
//                // vivo离线推送
//                thirdCode = "vivo"
////                PushClient.getInstance(applicationContext).initialize()
//            } else if (IMFunc.isBrandOppo()) {
//                thirdCode = "oppo"
//                // oppo离线推送
////                HeytapPushManager.init(this, true)
////                HeytapPushManager.register(this, PrivateConstants.OPPO_PUSH_APPKEY, PrivateConstants.OPPO_PUSH_APPSECRET, mPushCallback)
//
//            }
////            registerActivityLifecycleCallbacks(StatisticActivityLifecycleCallback())
//        }


//        val imEventListener = object : IMEventListener() {
//            override fun onNewMessages(msgs: List<TIMMessage>?) {
//                DemoLog.i(TAG, "****************************************** onNewMessages ******************************************")
////                CustomAVCallUIController.getInstance().onNewMessage(msgs)
//                if (CustomMessage.convert2VideoCallData(msgs) != null) {
////                    SystemHelper.setTopApp(MyApplication.getInstance().baseContext);
//                    // 会弹出接电话的对话框，不再需要通知
////                    waitTimMsg = msgs
////                    DemoLog.i(TAG, "record video msg")
//                    CustomAVCallUIController.getInstance().onNewMessage(msgs)
//                    return
//                }
//                for (msg in msgs!!) {
//                    // 小米手机需要在设置里面把demo的"后台弹出权限"打开才能点击Notification跳转。TIMOfflinePushNotification后续不再维护，如有需要，建议应用自己调用系统api弹通知栏消息。
//                    val notification = TIMOfflinePushNotification(this@MyApplication, msg)
//                    notification.doNotify(this@MyApplication, R.drawable.default_user_icon)
//                }
//            }
//        }
//        TUIKit.addIMEventListener(imEventListener)

        //友盟分享
//        UMShareAPI.get(this)
//        Config.DEBUG = true
        PlatformConfig.setWeixin(GlobalConfig.WeixinAppId, GlobalConfig.WeixinAppSecret) //新包

        GsonFactory.setJsonCallback(object : JsonCallback {
            override fun onTypeException(typeToken: TypeToken<*>?, fieldName: String?, jsonToken: JsonToken?) {
                var msg = "类型解析异常：" + typeToken + "#" + fieldName + "，后台返回的类型为：" + jsonToken
                Log.e("GsonFactory", msg)
                /** 新版本  */
//                PgyCrashManager.reportCaughtException(Exception(msg))
            }
        })
    }

    private fun initGeTuiSdk() {
        Log.d(TAG, "initializing GTsdk...")
        PushManager.getInstance().initialize(MyApplication.getInstance())
        if (BuildConfig.DEBUG) {
            //切勿在 release 版本上开启调试日志
            PushManager.getInstance().setDebugLogger(MyApplication.getInstance()) { s -> Log.i("PUSH_LOG", s) }
        }
    }

    private fun initPgyerSDK() {
//        PgyerSDKManager.Init()
//                .setContext(MyApplication.getInstance())
//                .start()
    }


}
